package com.davesandoval.tpdroid;

import java.io.IOException;
import org.apache.http.client.ClientProtocolException;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class LoginActivity extends Activity {
	private String logid = null;
	private String username = null;
	private String passwd = null;
	private String result = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		final String[] users = Main.client.getUser(Main.globalURI);
		final String[] lognum = Main.client.getUserId(Main.globalURI);

		Spinner s1 = (Spinner) findViewById(R.id.spinner1);
		Button b1 = (Button) findViewById(R.id.btnDoLogin);
		final EditText passwd = (EditText) findViewById(R.id.editText1);
		final CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, users);

		s1.setAdapter(adapter);
		s1.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				int index = arg0.getSelectedItemPosition();
				logid = lognum[index];
				username = users[index];
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				username = null;
			}
		});

		b1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String savedPassword = passwd.getText().toString();
				if ((savedPassword.equals(null)) || (savedPassword.equals(""))) {
					Toast.makeText(LoginActivity.this,
							"Please enter your password", Toast.LENGTH_SHORT)
							.show();
				} else if ((username.equals(null)) || (username.equals(""))) {
					Toast.makeText(LoginActivity.this,
							"Please select your username", Toast.LENGTH_SHORT)
							.show();
				} else {
					try {
						result = Main.client.doLogin(Main.globalURI, logid,
								passwd.getText().toString());
					} catch (ClientProtocolException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (result == "LoginSuccess") {
						Main.username = username;
						Toast.makeText(LoginActivity.this,
								"Successfully logged in!", Toast.LENGTH_SHORT)
								.show();
						Main.loggedIn = true;
						if (cb.isChecked()) {
							SharedPreferences prefs = getSharedPreferences(
									"USER_PREFS", 0);
							SharedPreferences.Editor editor = prefs.edit();
							editor.putString("username", username);
							editor.putString("logid", logid);
							editor.putString("passwd", savedPassword);
							editor.commit();
						}
						Intent in = new Intent();
						setResult(1, in);
						finish();

					} else {
						Toast.makeText(
								LoginActivity.this,
								"Unable to log you in, please verify your username and password",
								Toast.LENGTH_SHORT).show();
						Main.loggedIn = false;
						passwd.setText("");
					}
				}
			}
		});
	}

	@Override
	protected void onStop() {
		super.onStop();
	}
}